7.8 Early Initialization of the Linux Security Modules (LSM)
The Linux kernel maintains a global, consolidated tracking table infrastructure named security_hook_heads to archive individual hash list head descriptors. Every discrete list head within this database maps straight to a dedicated callback function vector (security hook), providing the subsystem with a centralized interface to locate and invoke active access control routines. The comprehensive taxonomy of security module callback hooks natively identified by the kernel is declared within the core header file include/linux/lsm_hook_defs.h. The architectural layout definition of the security_hook_heads structure is configured as follows:
struct security_hook_heads {
struct hlist_head binder_set_context_mgr;
int (*binder_set_context_mgr)(struct task_struct *mgr);
struct hlist_head binder_transaction;
int (*binder_transaction)(struct task_struct *from, struct task_struct *to);
/* ... Additional subsystem definitions omit for brevity ... */
struct hlist_head perf_event_write;
int (*perf_event_write)(struct perf_event *event);
} __randomize_layout;
The initial operation executed by the early security initialization function early_security_init() is to iterate across this master database and cleanly initialize each embedded hash list head structure (struct hlist_head) to a null baseline state.
The Linux kernel infrastructure can simultaneously accommodate a multi-layered matrix of disparate security modules, with each discrete module described by a dedicated struct lsm_info tracking layout:
struct lsm_info {
const char *name; /* Required: Unique identifier string */
enum lsm_order order; /* Optional: Defaults to LSM_ORDER_MUTABLE */
unsigned long flags; /* Optional: Operational flags describing the LSM */
int *enabled; /* Optional: State control indicator driven by CONFIG_LSM */
int (*init)(void); /* Required: Primary entry function pointer */
struct lsm_blob_sizes *blobs; /* Optional: Tracks metadata requirements for blob sharing */
};
During the project compilation step, the linker utility aggregates all initialized lsm_info descriptor structures and writes them contiguously into a specialized, low-level link section bounded by the symbols __start_early_lsm_info and __end_early_lsm_info.
The secondary technical objective managed within the early_security_init() lifecycle phase is the orchestration and execution of the early security modules. The parsing loop initially verifies whether the targeted LSM exposes a proprietary, configurable initialization tracking state variable (enabled). Following this lookup, it passes the descriptor block context over to the internal helper utility prepare_lsm() to determine whether the target module complies with system policies and can be safely activated. If a module lacks an explicit status controller reference, the subsystem initializes its evaluation flags using standard system fallback variables (lsm_enabled_true and lsm_enabled_false).
If the evaluation phase flags the target module as explicitly disabled, or if the controller demands exclusive platform enforcement (LSM_FLAG_EXCLUSIVE) when another exclusive security infrastructure has already finished initialization, the current candidate module is blocked from activating. Conversely, if the check validation confirms that the candidate module is eligible for activation, the engine invokes its primary entry function pointer (.init) to bring up the subsystem hardware dependencies.
The low-level helper function tasked with verifying module eligibility is structured as follows:
static void __init prepare_lsm(struct lsm_info *lsm)
{
int enabled = lsm_allowed(lsm);
set_enabled(lsm, enabled);
if (enabled) {
if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
exclusive = lsm;
init_debug("exclusive chosen: %s\n", lsm->name);
}
lsm_set_blob_sizes(lsm->blobs);
}
}
The core function tasked with invoking the module's compiled entry handler to execute the early subsystem initialization loops is structured as follows:
static void __init initialize_lsm(struct lsm_info *lsm)
static void __init initialize_lsm(struct lsm_info *lsm)
{
if (is_enabled(lsm)) {
int ret;
init_debug("initializing %s\n", lsm->name);
ret = lsm->init();
WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
}
}
This concludes Section 7.8 and the early initialization sequence of the Linux Security Modules.